//
//  testSendCode.m
//  HTPaymentFramework
//
//  Created by 内蒙小猿 on 2018/4/12.
//  Copyright © 2018年 zhaozhongpeng. All rights reserved.
//  

#import "HTPaymentInputSendCode.h"
#import "HTPaymentSendCodeLabel.h"

@interface HTPaymentInputSendCode ()<UITextFieldDelegate>



@property (nonatomic,strong) UITextField *InputNumTextField;

@property (nonatomic,strong) HTPaymentSendCodeLabel *fristLabel;

@property (nonatomic,strong) HTPaymentSendCodeLabel *secondLabel;

@property (nonatomic,strong) HTPaymentSendCodeLabel *thirdLabel;

@property (nonatomic,strong) HTPaymentSendCodeLabel *forthLabel;

@property (nonatomic,strong) HTPaymentSendCodeLabel *fifthLabel;

@property (nonatomic,strong) HTPaymentSendCodeLabel *sixthLabel;



@end

@implementation HTPaymentInputSendCode

- (instancetype)init{
    self = [super init];
    if (self) {
        [self createUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}


- (void)createUI{
    self.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSLog(@"宽比例 -- %f",[UIScreen mainScreen].bounds.size.width/375);
    
    [self addSubview:self.InputNumTextField];
    [self addSubview:self.fristLabel];
    [self addSubview:self.secondLabel];
    [self addSubview:self.thirdLabel];
    [self addSubview:self.forthLabel];
    [self addSubview:self.fifthLabel];
    [self addSubview:self.sixthLabel];
    
    NSLayoutConstraint *textFieldTop = [NSLayoutConstraint constraintWithItem:self.InputNumTextField attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];

    NSLayoutConstraint *textFieldLeft = [NSLayoutConstraint constraintWithItem:self.InputNumTextField attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:spaceNum];

    NSLayoutConstraint *textFieldRight = [NSLayoutConstraint constraintWithItem:self.InputNumTextField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-spaceNum];

    NSLayoutConstraint *textFieldHeight = [NSLayoutConstraint constraintWithItem:self.InputNumTextField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:textFieldHeightNum];

    [self addConstraints:@[textFieldTop,textFieldLeft,textFieldRight,textFieldHeight]];
    
    // 第一个
    NSLayoutConstraint *fristLabelTop = [NSLayoutConstraint constraintWithItem:self.fristLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.InputNumTextField attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    
    NSLayoutConstraint *fristLabelLeft = [NSLayoutConstraint constraintWithItem:self.fristLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.InputNumTextField attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    
    NSLayoutConstraint *fristLabelWidth = [NSLayoutConstraint constraintWithItem:self.fristLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:showLabelW];
    
    NSLayoutConstraint *fristLabelHeight = [NSLayoutConstraint constraintWithItem:self.fristLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:showLabelH];
    
    [self addConstraints:@[fristLabelTop,fristLabelLeft,fristLabelWidth,fristLabelHeight]];
    
    
    // 第二个
    [self addConstraints:[self addConstraintWith:self.secondLabel toItem:self.fristLabel]];
    // 第三个
    [self addConstraints:[self addConstraintWith:self.thirdLabel toItem:self.secondLabel]];
    // 第四个
    [self addConstraints:[self addConstraintWith:self.forthLabel toItem:self.thirdLabel]];
    // 第五个
    [self addConstraints:[self addConstraintWith:self.fifthLabel toItem:self.forthLabel]];
    // 第六个
    [self addConstraints:[self addConstraintWith:self.sixthLabel toItem:self.fifthLabel]];

    
    
}


- (NSArray *)addConstraintWith:(UIView *)item toItem:(UIView *)toItem{
    NSLayoutConstraint * Top = [NSLayoutConstraint constraintWithItem:item attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:toItem attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    
    NSLayoutConstraint * Left = [NSLayoutConstraint constraintWithItem:item attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:toItem attribute:NSLayoutAttributeRight multiplier:1.0 constant:spaceBetweenlabelNum];
    
    NSLayoutConstraint *Width = [NSLayoutConstraint constraintWithItem:item attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:toItem attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
    
    NSLayoutConstraint *Height = [NSLayoutConstraint constraintWithItem:item attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:toItem attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    
    return @[Top,Left,Width,Height];
}


- (UITextField *)InputNumTextField{
    
    if (_InputNumTextField == nil) {
        _InputNumTextField = [[UITextField alloc] init];
        _InputNumTextField.translatesAutoresizingMaskIntoConstraints = NO;
        _InputNumTextField.delegate = self;
        _InputNumTextField.backgroundColor = [UIColor clearColor];
        _InputNumTextField.textColor = [UIColor clearColor];
        _InputNumTextField.tintColor = [UIColor clearColor];
        _InputNumTextField.keyboardType = UIKeyboardTypeNumberPad;
        [_InputNumTextField becomeFirstResponder];
    }
    return _InputNumTextField;
    
}




- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSLog(@"%@ - %lu",string,(unsigned long)range.location);
    if (range.location > 5) {
        return NO;
    }
    
    switch (range.location) {
        case 0:{
            self.fristLabel.text = string;
            }
            break;
        case 1:{
            self.secondLabel.text = string;
        }
            break;
        case 2:{
            self.thirdLabel.text = string;
        }
            break;
        case 3:{
            self.forthLabel.text = string;
        }
            break;
        case 4:{
            self.fifthLabel.text = string;
        }
            break;
        case 5:{
            self.sixthLabel.text = string;
        }
            break;
            
        default:
            break;
    }
    
    return YES;
}



- (HTPaymentSendCodeLabel *)fristLabel{
    if (_fristLabel == nil) {
        _fristLabel = [[HTPaymentSendCodeLabel alloc] init];
    }
    return _fristLabel;
}

- (HTPaymentSendCodeLabel *)secondLabel{
    if (_secondLabel == nil) {
        _secondLabel = [[HTPaymentSendCodeLabel alloc] init];
    }
    return _secondLabel;
}

- (HTPaymentSendCodeLabel *)thirdLabel{
    if (_thirdLabel == nil) {
        _thirdLabel = [[HTPaymentSendCodeLabel alloc] init];
    }
    return _thirdLabel;
}

- (HTPaymentSendCodeLabel *)forthLabel{
    if (_forthLabel == nil) {
        _forthLabel = [[HTPaymentSendCodeLabel alloc] init];
    }
    return _forthLabel;
}

- (HTPaymentSendCodeLabel *)fifthLabel{
    if (_fifthLabel == nil) {
        _fifthLabel = [[HTPaymentSendCodeLabel alloc] init];
    }
    return _fifthLabel;
}

- (HTPaymentSendCodeLabel *)sixthLabel{
    if (_sixthLabel == nil) {
        _sixthLabel = [[HTPaymentSendCodeLabel alloc] init];
    }
    return _sixthLabel;
}



@end
